<?php
function comli($page_size = ADM_PAGESIZE) {
    global $_TABLES;

    $view_var           = array();
    $tbl_article_comment= $_TABLES['app'];
    $tbl_article    = $_TABLES['article'];

    $conn   = & get_db_conn();
    if($_GET['cata']&&!$_GET['aid']){
        $where  = "WHERE 1 and C.type={$_GET['cata']} ";
    }else{
        $where  = "WHERE 1 and C.type=0 ";
    }
    

    $aid    = @(int)$_GET['aid'];
    if($aid > 0) {
        // 显式指定了评论
        $where  .= "AND C.aid = $aid ";
    }


    // 分页处理开始
    $csql   = "SELECT COUNT(*) AS rs_count FROM $tbl_article_comment AS C $where ";
    $rs     = $conn->GetRow($csql);


    $record_total   = @(int)$rs['rs_count'];
    $page_total     = ceil($record_total/$page_size);

    $page   = @(int)$_GET['page'];
    if($page < 1) $page = 1;
    if($page > $page_total && $page_total > 0) $page = $page_total;


    $view_var['record_total']   = $record_total;
    $view_var['page_total']     = $page_total;
    $view_var['page']           = $page;
    $view_var['page_size']      = $page_size;
    // 分页处理完毕


    // 列表处理开始
    $order_by   = trim(@$_GET['orderby']);
    $order_desc = @(int)$_GET['desc'];

    $str_order_by   = '';
    if($order_by != '') {
        $order_desc = ($order_desc!=1)?'ASC':'DESC';
        $str_order_by = "C.`{$order_by}` {$order_desc}, ";
    }

    $sql    = "SELECT C.*, A.cid, A.article_title , A.display
                        FROM $tbl_article_comment AS C 
                   LEFT JOIN $tbl_article AS A 
                          ON C.aid = A.aid 

            $where 
            ORDER BY $str_order_by C.id DESC ";
    //echo $sql;
    $rs = $conn->PageExecute($sql, $page_size, $page);
    $view_var['record'] = $rs->GetRows();
    // 列表处理结束


    return $view_var;
}
function comread($id) {
    global $_TABLES;
    $tbl_article_comment    = $_TABLES['app'];
    $tbl_article    = $_TABLES['article'];
    $tbl_mem    = $_TABLES['members'];
    if($id > 0) {
        $sql    = "SELECT C.*,A.article_title FROM $tbl_article_comment AS C 
                   LEFT JOIN $tbl_article AS A  ON C.aid = A.aid  WHERE C.id = '$id' ";
        $conn   = & get_db_conn();
        $view_var   = $conn->GetRow($sql);
        $sql="select * from $tbl_mem where uid={$view_var['uid']}";
        $view_var['user']   = $conn->GetRow($sql);
    } else {
        $view_var   = array();
    }
    return $view_var;
}

function comcheck($check) {
    global $_TABLES, $timestamp, $class_var, $G_admin_session_key;
    $tbl_article_comment    = $_TABLES['app'];


    if(@(int)$check < 1) {
        $check  = 0;
        $checklang  = '取消审核';
    } else {
        $check  = 1;
        $checklang  = '通过审核';
    }

    $item_id    = @(int)$_GET['item_id'];
    if($item_id <= 0) {
        $item_id = array_join_comma($_POST['item_id']);
    } else {
        $item_id = (string)$item_id;
    }
    if($item_id != '') {
        $conn   = & get_db_conn();
        $update_sql = "update $tbl_article_comment SET 
            display = $check 
            WHERE id IN ($item_id)";
        if($conn->Execute($update_sql)) {
            js_success('指定评论' . $checklang . '成功！');
        }
    }
    exit();
}


function commrequest() {
    global $timestamp, $G_big_integer;
    $data = $_POST['comment'];

    $data['display'] = @(int)$data['display'];

    $arr    = explode('-', $data['reply_date']);
   
    if($GLOBALS['ERR']->Count > 0)
        return false;
    else {
        /**
         * 接受数据前的处理工作，例如上传等
         *
         */
        /*global $mod, $G_abs_includes;
        require($G_abs_includes . '/upload.php');
        $up = upload_files($mod);

        $data['img'] = '';
        $data['up_img'] = '';
        if(!$data['delimg'] && $up) {
            $data['up_img']= empty($up['img']) ? '' : $up['img'];
        }*/
        //print_r($_POST);exit;
        return $data;
    }
}



function comadd() {
    global $_TABLES, $timestamp;

    $tbl_article_comment = $_TABLES['app'];

    $result = commrequest();
    if($result !== false) {
        $conn   = & get_db_conn();

            unset($result['id']);
            if($conn->AutoExecute($tbl_article_comment, $result, 'INSERT')) {
                $id = $conn->Insert_ID();
                return $id;
            }
    }
    return false;
}

function commodify() {
    global $_TABLES, $timestamp, $mod;

    $tbl_article_comment = $_TABLES['app'];
    $result = commrequest();

    if($result !== false) {
        $conn   = & get_db_conn();

            if($conn->AutoExecute($tbl_article_comment, $result,'UPDATE', 'id=' . $result['id'] )) {
                // 返回到控制器的内容
                $re = array('id'=>$result['id'],'aid'=>$result['aid'],'cata'=>$result['cata']);
                return $re;
            }
    }
    //return false;
}


function comdel() {
    global $_TABLES, $timestamp, $class_var, $G_admin_session_key;
    $tbl_article_comment    = $_TABLES['app'];


    $item_id    = @(int)$_GET['item_id'];
    if($item_id <= 0) {
        $item_id = array_join_comma($_POST['item_id']);
    } else {
        $item_id = (string)$item_id;
    }
    if($item_id != '') {
        $conn   = & get_db_conn();
        $update_sql = "DELETE FROM $tbl_article_comment WHERE id IN ($item_id)";
        if($conn->Execute($update_sql)) {
            js_success('删除成功！');
        }
    }
    exit();
}


?>